﻿using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Skybot.Arcgis.Client.TileLayerExpand
{
    /// <summary>
    /// MAPabc扩展
    /// </summary>
    public class TiledLayerMapAbcExpand : ESRI.ArcGIS.Client.TiledMapServiceLayer
    {

          #region 私有變數

        private string uriTemplate = "http://emap2.mapabc.com/mapabc/maptile?x={0}&y={1}&z={2}";

        private const int WKID = 0x18ee1;


        /// <summary>
        ///  全地圖最大顯示區域
        /// </summary>
        private Envelope _fullExtent = null;

        /// <summary>
        ///  本地圖最大放大倍數
        /// </summary>
        private int _MaxLevel = 22;
        /// <summary>
        ///  本地圖最小放大倍數
        /// </summary>
        private int _MinLevel = 1;
        #endregion

        #region 公共屬性
        /// <summary>
        /// 改變URL值
        /// </summary>
        public string UriTemplate
        {
            get { return uriTemplate; }
            set { uriTemplate = value; }
        }
        #endregion
        #region 構造函數



        /// <summary>
        /// 調用 默認的 在線地圖創建圖層
        /// </summary>
        public TiledLayerMapAbcExpand()
        {
            base.SpatialReference = new SpatialReference(WKID);
        }

        /// <summary>
        /// 創建一個新的對像
        /// </summary>
        /// <param name="RoadUriBuilder">示例 new UriBuilder("http://emap2.mapabc.com/mapabc/maptile?x={0}&y={1}&z={2}");</param>
        public TiledLayerMapAbcExpand(Uri RoadUriBuilder)
        {
            //線路URL
            uriTemplate = RoadUriBuilder.ToString();
            base.SpatialReference = new SpatialReference(WKID);
        }



        /// <summary>
        ///構造一個圖層
        /// </summary>
        /// <param name="RoadUriBuilder">示例 new UriBuilder("http://emap2.mapabc.com/mapabc/maptile?x={0}&y={1}&z={2}");</param>
        /// <param name="FullExtent">本地圖區域</param>
        public TiledLayerMapAbcExpand(Uri RoadUriBuilder, Envelope FullExtent)
        {
            //線路URL
            uriTemplate = RoadUriBuilder.ToString();
            /// <summary>
            ///  全地圖最大顯示區域
            /// </summary>
            this.FullExtent = FullExtent;
            /// <summary>
            ///  全地圖最大顯示區域
            /// </summary>
            _fullExtent = FullExtent;
            base.SpatialReference = new SpatialReference(WKID);
        }

        /// <summary>
        ///構造一個圖層
        /// </summary>
        /// <param name="RoadUriBuilder">示例 new UriBuilder("http://emap2.mapabc.com/mapabc/maptile?x={0}&y={1}&z={2}");</param>
        /// <param name="FullExtent">本地圖區域</param>
        /// <param name="MaxLeval">最大縮放級別</param>
        /// <param name="MinLeval">最小縮放級別</param>
        public TiledLayerMapAbcExpand(Uri RoadUriBuilder, Envelope FullExtent, int MaxLevel, int MinLevel)
        {
            //線路URL
            uriTemplate = RoadUriBuilder.ToString();
            this.FullExtent = FullExtent;
            /// <summary>
            ///  全地圖最大顯示區域
            /// </summary>
            _fullExtent = FullExtent;
            /// <summary>
            ///  本地圖最大放大倍數
            /// </summary>
            _MaxLevel = MaxLevel;
            /// <summary>
            ///  本地圖最小放大倍數
            /// </summary>
            _MinLevel = MinLevel;
            base.SpatialReference = new SpatialReference(WKID);
        }

        /// <summary>
        ///構造一個圖層
        /// </summary>
        /// <param name="RoadUriBuilder">示例 new UriBuilder("http://emap2.mapabc.com/mapabc/maptile?x={0}&y={1}&z={2}")</param>
        /// <param name="FullExtent">本地圖區域</param>
        /// <param name="MaxLeval">最大縮放級別</param>
        /// <param name="MinLeval">最小縮放級別</param>
        public TiledLayerMapAbcExpand(Uri RoadUriBuilder, int MaxLevel, int MinLevel)
        {
            //線路URL
            uriTemplate = RoadUriBuilder.ToString();
            /// <summary>
            ///  本地圖最大放大倍數
            /// </summary>
            _MaxLevel = MaxLevel;
            /// <summary>
            ///  本地圖最小放大倍數
            /// </summary>
            _MinLevel = MinLevel;
            base.SpatialReference = new SpatialReference(WKID);
        }

        #endregion

        /// <summary>
        /// 計劃算出 get URL
        /// </summary>
        /// <param name="level">放大等級</param>
        /// <param name="row">行</param>
        /// <param name="col">列</param>
        /// <returns></returns>
        public override string GetTileUrl(int level, int row, int col)
        {



            return string.Format(uriTemplate, col, row, level+1);
        }

        /// <summary>
        /// 得到圖層的方法
        /// </summary>
        public override void Initialize()
        {

            //1、Tiling Scheme Origin；
            //2、切图的范围，也就是FullExtent；
            //3、SpatialReference；
            //4、TileInfo，包括切图的大小，级数，以及每级的Resolution；
            //5、最后就是重写GetTileUrl方法。



            ///默認最大最小放大倍數
            int from = _MinLevel;
            int to = _MaxLevel;
            TileInfo info = new TileInfo();
            info.SpatialReference = new SpatialReference(WKID);
            info.Origin = new MapPoint(-20037508.3427892, 20037508.3427892);
            #region 用戶初始化處理
            //如果用戶沒有初始
            if (_fullExtent == null)
            {

                this.FullExtent = new Envelope(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892);

            }
            else
            {
                //指定地圖範圍
                info.Origin = new MapPoint(180 * 4, -180 * 4);
            }

            #endregion

            info.Width = 256;
            info.Height = 256;
            info.Lods = new Lod[(to - from) + 2];
            base.TileInfo = info;

            int num3 = 2 * from;
            int num4 = 2 * from;
            double num5 = (this.FullExtent.Width / ((double)num3)) / ((double)base.TileInfo.Width);

            //初始化數組 用於處理 載入的級別
            //處理最大的放大級別. 這裏采用最大放大級別
            Lod[] loadArray = new Lod[to];
            for (int i = 0; i < to; i++)
            {
                Lod lod = new Lod();
                lod.Resolution = num5;
                loadArray[i] = lod;
                num4 *= 2;
                num3 *= 2;
                num5 *= 0.5;
            }
            //初始化放大等級總數
            base.TileInfo.Lods = loadArray;


            

            base.InitializationFailure = null;
            base.Initialize();

        }

    }
}
